const fs = require("fs");
const path = require("path");

const basePath = "/static/avatar/";
let avatarList;
// 获取所有头像文件名
(async () => {
    avatarList = await fs.promises.readdir(path.resolve(__dirname, "../public/static/avatar"));
})()

exports.formatResponseBody = ({ code: code = 0, msg: msg = "", data: data = null }) => {
    return {
        code,
        msg,
        data
    }
}

exports.filterObjProp = (obj, props) => {
    for (const key in obj) {
        if (Object.hasOwnProperty.call(obj, key)) {
            if (!props.includes(key)) {
                delete obj[key];
            }
        }
    }
    return obj;
}

exports.getRandomAvatarPath = () => {
    const index = Math.floor(Math.random() * avatarList.length);
    return basePath + avatarList[index];
}